package starsservice.com.kc.controller;


import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.common.Util.ParamEnum;
import common.common.Util.ResponseBean;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import starsservice.com.kc.bean.Account;
import starsservice.com.kc.service.PhoneGetService;
import starsservice.com.kc.service.RegistAndLoginService;

import javax.annotation.Resource;


@RestController
@RequestMapping("stars")
@Api(value = "登陆注册接口",tags = "user",description = "用户登陆以及注册的接口")
public class RegistAndLoginController {

    @Resource
    private PhoneGetService phoneGetService;

    @Resource
    private RegistAndLoginService registAndLoginService;

    /**
     * 获取验证码
     * @param phone
     * @return
     * @throws ClientException
     */
    @GetMapping("getphonecode")
    @ApiOperation(value = "短信验证码，同一号码一天限制10次",notes = "短信验证码")
    @ApiResponses({
            @ApiResponse(code = 200,message = "success"),
            @ApiResponse(code = 400,message = "error")
    })
    public void getphonecode(@ApiParam(name = "phone",value = "手机号",required = true) String phone) throws ClientException {
        if (phone.isEmpty()){
            return;
        }
        phoneGetService.PhoneGet(phone, ParamEnum.regist_id);
    }


    @GetMapping("test")
    public ResponseBean test(){
        Account account = new Account();
        return new ResponseBean(200,"test",account);
    }

    @ApiOperation(value = "用户注册",notes = "{\n" +
            "    \"account\": {\n" +
            "        \"accountPhone\": \"13537447981\",\n" +
            "        \"accountName\": \"我是测试\",\n" +
            "        \"accountSex\": \"男\"\n" +
            "    },\n" +
            "    \"code\": \"123\",\n" +
            "    \"password\": \"123456\"\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "注册成功"),
            @ApiResponse(code = 400,message = "验证码错误"),
            @ApiResponse(code = 400,message = "注册失败请重试查看资料是否填写完毕"),
            @ApiResponse(code = 400,message = "注册失败请重试")
    })
    @PostMapping("regist")
    public ResponseBean regist(@RequestBody String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Account.RegistAccount registAccount = mapper.readValue(json, Account.RegistAccount.class);
        Account account = registAccount.getAccount();
        return registAndLoginService.regist(account,registAccount.getCode(),registAccount.getPassword());
    }


    @ApiOperation(value = "手机号登陆",notes = "手机号登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 401,message = "登陆失败请重试"),
            @ApiResponse(code = 400,message = "密码错误请重试")
    })
    @PostMapping("phonelogin")
    public ResponseBean phonelogin(String phone,String password){
        return registAndLoginService.LoginPhone(phone,password);
    }


    @ApiOperation(value = "小米账号登陆",notes = "小米账号登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobnumber",value = "小米账号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 403,message = "登陆失败请重试"),
            @ApiResponse(code = 402,message = "密码错误请重试")
    })
    @PostMapping("jobnumberlogin")
    public ResponseBean jobnumberlogin(String jobnumber,String password){
        return registAndLoginService.LoginJoBnumber(jobnumber, password);
    }
}
